package com.example.hospital.mapper;

import com.example.hospital.domain.entity.ExamPackage;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ExamPackageMapper {
    @Insert("INSERT INTO package (package_name, description, total_price, create_time) VALUES (#{packageName}, #{description}, #{totalPrice}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "packageId")
    int insert(ExamPackage pkg);

    @Update("UPDATE package SET package_name=#{packageName}, description=#{description}, total_price=#{totalPrice} WHERE package_id=#{packageId}")
    int update(ExamPackage pkg);

    @Delete("DELETE FROM package WHERE package_id=#{packageId}")
    int delete(@Param("packageId") Long packageId);

    @Select("SELECT * FROM package WHERE package_id=#{packageId}")
    ExamPackage findById(@Param("packageId") Long packageId);

    @Select({
            "<script>",
            "SELECT * FROM package",
            "<where>",
            "  <if test=\"packageName != null and packageName != ''\"> AND package_name LIKE CONCAT('%', #{packageName}, '%') </if>",
            "</where>",
            "ORDER BY create_time DESC",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<ExamPackage> list(@Param("packageName") String packageName, @Param("limit") int limit, @Param("offset") int offset);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM package",
            "<where>",
            "  <if test=\"packageName != null and packageName != ''\"> AND package_name LIKE CONCAT('%', #{packageName}, '%') </if>",
            "</where>",
            "</script>"
    })
    long count(@Param("packageName") String packageName);
}